package com.smc.demo.activitydemo;

import android.app.Application;
import android.os.SystemClock;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;

import com.smc.demo.activity.utils.ActivityThreadUtil;
import com.smc.demo.activity.utils.InstrumentationUtil;

public class MainActivity extends AppCompatActivity {
    private  final String TAG = "MainActivity";
    private  int metaState = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event){
        Log.i(TAG, "onKeyDown keyCode: " + keyCode);
        return super.onKeyDown(keyCode, event);
    }

    @Override
    public boolean onKeyUp(int keyCode, KeyEvent event){
        Log.i(TAG, "onKeyUp keyCode: " + keyCode);
        return super.onKeyUp(keyCode, event);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        metaState = event.getMetaState();
        Log.i(TAG, "onTouchEvent x: " + event.getX() + " ,y: " + event.getY());
        return super.onTouchEvent(event);
    }

    public void onGetGlobalAppBtn(View v){

        Application app1 = getApplication();
        Application app2 = ActivityThreadUtil.getInstance().getApplication();
        if (app1 == app2){
            Log.i(TAG, "is the same app");
        }else{
            Log.i(TAG, "is not the same app");
        }
        Runnable runnable = new Runnable() {
            @Override
            public void run() {
                //发送按键消息
                KeyEvent keyEventDown = new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_0);
                InstrumentationUtil.sendKeySync(keyEventDown);
                SystemClock.sleep(20);
                KeyEvent keyEventUp = new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_0);
                InstrumentationUtil.sendKeySync(keyEventUp);

                //发送点击消息
                long downTime = SystemClock.uptimeMillis();
                long eventTime = SystemClock.uptimeMillis();
                int action = MotionEvent.ACTION_DOWN;
                float x = 100;
                float y = 200;
                MotionEvent motionEvent = MotionEvent.obtain(downTime, eventTime, action, x, y, metaState);
                InstrumentationUtil.sendPointerSync(motionEvent);
            }
        };
        Thread th = new Thread(runnable);
        th.start();
    }

}
